package com.tul.exercise.base.recursion;


import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author 第五天
 *         递归的树形节点工具类
 */
public class EMSSimpleTreeUtils {

    /**
     * 根据类型和父节点id创建节点
     *
     * @param type 节点类型
     * @param pid  父节点id
     * @return 节点
     */
    public static EMSSimpleTree createEMSSimpleTree(int type, long pid) {
        EMSSimpleTree result = new EMSSimpleTree();
        // 以毫秒数做为id
        result.setId(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        result.setType(type);
        result.setPid(pid);
        return result;
    }

    /**
     * 创建多个节点
     *
     * @param num  节点个数
     * @param type 节点类型
     * @param pid  父节点id
     * @return 节点数组
     */
    public static EMSSimpleTree[] createEMSSimpleTrees(int num, int type, long pid) {
        EMSSimpleTree[] result = new EMSSimpleTree[num];
        for (int i = 0; i < num; i++) {
            result[i] = createEMSSimpleTree(type, pid);
        }
        return result;
    }

    /**
     * 创建节点list
     *
     * @param num  节点个数
     * @param type 节点类型
     * @param pid  节点父节点id
     * @return 节点列表
     */
    public static List<EMSSimpleTree> createEMSSimpleTreeList(int num, int type, long pid) {
        EMSSimpleTree[] nodes = createEMSSimpleTrees(num, type, pid);
        return Arrays.asList(nodes);
    }

    /**
     * 获取树形节点上类型为4的节点List
     * @param tree 树形节点
     * @return
     */
    public static List<EMSSimpleTree> getEMSSimpleTreeList(EMSSimpleTree tree) {
        List<EMSSimpleTree> result = new ArrayList<>();
        getNodesByRecursion(result, tree);
        return result;
    }

    /**
     * 获取树形节点上指定类型的节点List
     * @param tree 树形节点
     * @param type 指定类型
     * @return 节点List
     */
    public static List<EMSSimpleTree> getEMSSimpleTreeList(EMSSimpleTree tree, int type) {
        List<EMSSimpleTree> result = new ArrayList<>();
        getNodeListByType(result, tree, type);
        return result;
    }

    /**
     * 获取属性节点上类型为4 的节点List
     * @param nodeList 节点Lsit
     * @param node 属性节点
     */
    public static void getNodesByRecursion(List<EMSSimpleTree> nodeList, EMSSimpleTree node) {
        int type = node.getType();
        EMSSimpleTree[] nodes = node.getItems();

        if (type < 4 && nodes != null) {
            for (EMSSimpleTree item : nodes) {
                getNodesByRecursion(nodeList, item);
            }

        }

        if (type == 4) {
            nodeList.add(node);
        }
    }

    /**
     * 获取树形节点上指定类型的节点List
     * @param nodeList 节点List
     * @param node 树形节点
     * @param type 指定类型
     */
    public static void getNodeListByType(List<EMSSimpleTree> nodeList, EMSSimpleTree node, int type) {
        int nodeType = node.getType();
        EMSSimpleTree[] nodes = node.getItems();

        if (nodeType != type && nodes != null) {
            for (EMSSimpleTree item : nodes) {
                getNodeListByType(nodeList, item, type);
            }

        }

        if (nodeType == type) {
            nodeList.add(node);
        }
    }

}
